package com.ruoyi.web.controller.mdm;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.ruoyi.mdm.request.ChannelWeightConfig;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mdm.domain.ChannelWeight;
import com.ruoyi.mdm.service.IChannelWeightService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 渠道权重Controller
 *
 * @author shiseido
 * @date 2025-02-28
 */
@RestController
@RequestMapping("/mdm/channelWeight")
public class ChannelWeightController extends BaseController {
    @Autowired
    private IChannelWeightService channelWeightService;

    /**
     * 查询渠道权重列表
     */
    @ApiOperation("查询渠道权重列表")
    @PreAuthorize("@ss.hasPermi('mdm:channelWeight:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChannelWeight channelWeight) {
        startPage();
        List<ChannelWeight> list = channelWeightService.selectChannelWeightList(channelWeight);
        return getDataTable(list);
    }

    /**
     * 权重规则配置
     */
    @ApiOperation("权重规则配置")
    @PreAuthorize("@ss.hasPermi('mdm:channelWeight:config')")
    @Log(title = "权重规则", businessType = BusinessType.UPDATE)
    @PostMapping("/config")
    public void config(@RequestBody ChannelWeightConfig channelWeightConfig) {

        List<ChannelWeight> channelWeightList = channelWeightConfig.getChannelWeights();

//        // 检查权重之和是否 100
//        BigDecimal totalWeightValue = new BigDecimal(0);
//        for (ChannelWeight channelWeight : channelWeightList) {
//            if (channelWeight.getChannelWeightValue() == null) {
//                continue;
//            }
//            totalWeightValue = totalWeightValue.add(channelWeight.getChannelWeightValue());
//        }
//        if (totalWeightValue.compareTo(new BigDecimal(100)) != 0) {
//            throw new ServiceException("渠道指标权重之和必须为100！");
//        }

        // 先根据渠道编码删除 渠道权重
        channelWeightService.deleteChannelWeightByChannelCode(channelWeightConfig.getChannelCode());

        // 然后新增
        for (ChannelWeight channelWeight : channelWeightList) {
            if (channelWeight.getChannelWeightValue() == null) {
                continue;
            }
            channelWeight.setStatus(channelWeightConfig.getStatus());
            channelWeight.setChannelCode(channelWeightConfig.getChannelCode());
            channelWeight.setCreateBy(getUsername());
            channelWeight.setUpdateBy(getUsername());
            channelWeightService.insertChannelWeight(channelWeight);
        }
    }

    /**
     * 导出渠道权重列表
     */
    @ApiOperation("导出渠道权重列表")
    @PreAuthorize("@ss.hasPermi('mdm:channelWeight:export')")
    @Log(title = "渠道权重", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChannelWeight channelWeight) {
        List<ChannelWeight> list = channelWeightService.selectChannelWeightList(channelWeight);
        ExcelUtil<ChannelWeight> util = new ExcelUtil<ChannelWeight>(ChannelWeight.class);
        util.exportExcel(response, list, "渠道权重数据");
    }

    /**
     * 获取渠道权重详细信息
     */
    @ApiOperation("获取渠道权重详细信息")
    @PreAuthorize("@ss.hasPermi('mdm:channelWeight:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(channelWeightService.selectChannelWeightById(id));
    }

    /**
     * 新增渠道权重
     */
    @ApiOperation("新增渠道权重")
    @PreAuthorize("@ss.hasPermi('mdm:channelWeight:add')")
    @Log(title = "渠道权重", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChannelWeight channelWeight) {
        return toAjax(channelWeightService.insertChannelWeight(channelWeight));
    }

    /**
     * 修改渠道权重
     */
    @ApiOperation("修改渠道权重")
    @PreAuthorize("@ss.hasPermi('mdm:channelWeight:edit')")
    @Log(title = "渠道权重", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChannelWeight channelWeight) {
        return toAjax(channelWeightService.updateChannelWeight(channelWeight));
    }

    /**
     * 删除渠道权重
     */
    @ApiOperation("删除渠道权重")
    @PreAuthorize("@ss.hasPermi('mdm:channelWeight:remove')")
    @Log(title = "渠道权重", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(channelWeightService.deleteChannelWeightByIds(ids));
    }
}
